package com.pet.cart.controller;


import com.pet.api.dto.OrderDetailDTO;
import com.pet.cart.domain.dto.CartItemDTO;
import com.pet.cart.domain.po.Cart;
import com.pet.cart.domain.vo.CartVO;
import com.pet.cart.service.ICartService;
import com.pet.common.domain.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-07-11
 */
@Api(tags = "购物车服务接口")
@RestController
@RequestMapping("/carts")
@RequiredArgsConstructor
public class CartController {
    private final ICartService cartService;

    @PostMapping
    @ApiOperation(value = "添加购物车")
    public Result addItem2Cart(@RequestBody CartItemDTO cartItemDTO) {
        boolean isSucceed = cartService.addItem2Cart(cartItemDTO);
        return isSucceed ? Result.success() : Result.error();
    }

    @GetMapping
    @ApiOperation(value = "获取购物车列表")
    public Result getCartItems() {
        List<CartVO> cartVOList = cartService.getCartItemsByUser();
        return Result.success(cartVOList);
    }

    @PutMapping
    @ApiOperation(value = "更新购物车数据")
    public Result updateCartItem(@RequestBody Cart cart) {
        boolean isSucceed = cartService.updateById(cart);
        return isSucceed ? Result.success() : Result.error();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除购物车数据")
    public Result deleteCartItem(@PathVariable Integer id) {
        boolean isSucceed = cartService.removeById(id);
        return isSucceed ? Result.success() : Result.error();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "批量删除购物车数据")
    public Result batchDeleteCartItem(@RequestBody List<Integer> ids) {
        boolean isSucceed = cartService.removeByIds(ids);
        return isSucceed ? Result.success() : Result.error();
    }

    @PostMapping("/order-remove")
    @ApiOperation(value = "下单时删除购物车数据")
    public boolean removeItemsByIds(@RequestParam("ids") List<Integer> ids) {
        return cartService.removeItemsByIds(ids);
    }

    @PostMapping("/order-add")
    @ApiOperation(value = "退单时恢复购物车数据")
    public void addItemsByIds(@RequestBody List<OrderDetailDTO> detailDTOList) {
        cartService.addItemsByIds(detailDTOList);
    }
}
